import Vue from 'vue'
import 'amfe-flexible'
import './assets/style.css'
import { Button, Cell, CellGroup, Field, Toast, NumberKeyboard, Picker, Popup, Lazyload, Dialog } from 'vant'
import App from './App.vue'
import router from './router'
import { httpPost } from './libs/http'
import { unixTimestamp } from './libs/utils'

Vue.use(Button)
Vue.use(Cell)
Vue.use(CellGroup)
Vue.use(Field)
Vue.use(Toast)
Vue.use(NumberKeyboard)
Vue.use(Picker)
Vue.use(Popup)
Vue.use(Lazyload)
Vue.use(Dialog)
Toast.setDefaultOptions({
  duration: 1000
})

Vue.prototype.$http = httpPost


//window.localStorage.openId = 'oCHCi0szftPkVGKUUVZ6vR2szBuw'
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (window.localStorage.openId || to.query.openId) {
      let profile = window.localStorage.profile
      if (to.query.openId) {
        window.localStorage.openId = to.query.openId
        window.localStorage.unixTime = unixTimestamp()
      }
      if(to.query.profile && (!profile || profile === 0 || profile === undefined)) {
        window.localStorage.profile = to.query.profile
      }
      next()
    } else {
      window.location = 'http://server.amyvacc.com/wx/oauth?redirect=' + encodeURIComponent(window.location.href)
      //window.location = 'http://aim.lycode.com/wx/oauth?redirect=' + encodeURIComponent(window.location.href)
    }
  } else {
    next()
  }
  next()
})

new Vue({
  el: '#app',
  router,
  render: h => h(App)
});
